﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Linq;
using System.Text;
using System.Web;
using System.Net;
using System.IO;


namespace BitcoinCommon
{
    public class HttpCommunication
    {
        public static string GetRequestData(string strURL)
        {
            string strReturn = "";

            strReturn = GetRequestData(strURL, "");

            return strReturn;
        }

        public static string GetRequestData(string strURL, string strParameter)
        {
            string strReturn = "";
            HttpWebRequest request;

            try
            {
                request = (HttpWebRequest)WebRequest.Create(new Uri(strURL));
                request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
                request.ContentLength = strParameter.Length;

                //데이터 요청 타입 설정
                if (strParameter.Length == 0)
                {
                    request.Method = "GET";
                }
                else
                {
                    request.Method = "POST";

                    Stream stDataParams = request.GetRequestStream();
                    byte[] byteString = Encoding.UTF8.GetBytes(strParameter);
                    stDataParams.Write(byteString, 0, byteString.Length);
                    stDataParams.Close();
                }

                // 요청, 응답 받기
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                // 응답 Stream 읽기
                Stream stReadData = response.GetResponseStream();
                StreamReader srReadData = new StreamReader(stReadData, Encoding.UTF8);

                // 응답 Stream -> 응답 String 변환
                string strResult = srReadData.ReadToEnd();

                //디코딩
                strReturn = HttpUtility.UrlDecode(strResult);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }


            return strReturn;
        }

        public static string GetRequestDataHeader(string strURL, string strParameter, Dictionary<string, string> dicHttpHeader)
        {
            string strReturn = "";
            HttpWebRequest request;

            try
            {
                request = (HttpWebRequest)WebRequest.Create(new Uri(strURL));
                request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
                request.ContentLength = strParameter.Length;

                if (dicHttpHeader != null)
                {
                    //Header 정보 세팅
                    foreach (KeyValuePair<string, string> oItem in dicHttpHeader)
                    {
                        request.Headers.Add(oItem.Key, oItem.Value);
                    }
                }

                //데이터 요청 타입 설정
                if (strParameter.Length == 0)
                {
                    request.Method = "GET";
                }
                else
                {
                    request.Method = "POST";

                    Stream stDataParams = request.GetRequestStream();
                    byte[] byteString = Encoding.UTF8.GetBytes(strParameter);
                    stDataParams.Write(byteString, 0, byteString.Length);
                    stDataParams.Close();
                }

                // 요청, 응답 받기
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                // 응답 Stream 읽기
                Stream stReadData = response.GetResponseStream();
                StreamReader srReadData = new StreamReader(stReadData, Encoding.UTF8);

                // 응답 Stream -> 응답 String 변환
                string strResult = srReadData.ReadToEnd();

                //디코딩
                strReturn = HttpUtility.UrlDecode(strResult);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return strReturn;
        }

        public static HMACSHA512 HMACSHA512Encript(string strData)
        {
            return new HMACSHA512(Encoding.ASCII.GetBytes(strData));
        }

        public static string GetHttpSign(string strClientSecretKey, string strURLParameter)
        {
            string strReturn = "";

            HMACSHA512 oEncrypt = HMACSHA512Encript(strClientSecretKey);
            strReturn = BitConverter.ToString(oEncrypt.ComputeHash(Encoding.ASCII.GetBytes(strURLParameter))).Replace("-", "").ToLower();

            return strReturn;
        }

        public static string ConvertURLParameter(Dictionary<string, string> dicParameter)
        {
            string strReturn = "";
            string strKey;
            string strValue;

            foreach (KeyValuePair<String, String> key in dicParameter)
            {
                //key와 value
                strKey = key.Key;
                strValue = key.Value;

                //URL Encoding
                strKey = HttpUtility.UrlEncode(strKey);
                strValue = HttpUtility.UrlEncode(strValue);

                if (strReturn.Length == 0)
                {
                    strReturn = strKey + "=" + strValue;
                }
                else
                {
                    strReturn = strReturn + "&" + strKey + "=" + strValue;
                }

            }

            return strReturn;
        }

        public static Dictionary<string, string> GetHttpHeaderAPI(string strClientIDKey, string strClientSecretKey, string strURLParameter)
        {
            Dictionary<string, string> dicHeader = new Dictionary<string, string>();

            dicHeader.Add("Key", strClientIDKey);
            dicHeader.Add("Sign", GetHttpSign(strClientSecretKey, strURLParameter));

            return dicHeader;
        }

    }
}
